<template>
	<view class="address-edit">
		<view class="form-box">
			<u--form ref="uForm" :model="form" :rules="rules" labelPosition="left" labelWidth="136rpx">
				<u-form-item label="联系人" prop="name" borderBottom>
					<u--input v-model="form.name" placeholder="收货人姓名" border="none"></u--input>
				</u-form-item>
				<u-form-item label="手机号" prop="phone" borderBottom>
					<u--input v-model="form.phone" type="tel" placeholder="收货人电话" border="none"></u--input>
				</u-form-item>
				<picker mode="region" @change="addressChange">
					<u-form-item label="选择地址" prop="address" borderBottom>
						<u--input v-model="form.address" placeholder="选择省/市/区/街道" border="none" disabled disabledColor="#ffffff"></u--input>
						<u-icon slot="right" name="arrow-right"></u-icon>
					</u-form-item>
				</picker>
				<u-form-item label="详细地址" prop="addr">
					<u--input v-model="form.addr" placeholder="街道地址、门牌号、小区等" border="none"></u--input>
				</u-form-item>
			</u--form>
		</view>
		
		<view class="default-box">
			<text class="label">设为默认地址</text>
			<switch type="switch" :checked="form.default" color="#9e2024" @change="defaultChange"/>
		</view>
		
		<view class="button-btn">
			<button class="delete-btn" @click="deleteAddress">删除</button>
			<button class="btn" @click="submit">提交</button>
		</view>
	</view>
</template>

<script>
	import { getRegion, editAddress, deleteAddress } from '@/api/api'

	export default {
		data () {
			return {
				form: {
					id: '',
					name: '',
					phone: '',
					address: '',
					region: '',
					addr: '',
					is_default: 0
				},
				rules: {
					name: { required: true, message: '请输入收货人姓名', trigger: 'blur' },
					phone: [
						{required: true, message: '请输入 11 位手机号', trigger: 'blur'},
						{ validator: (rule, value, callback) => { return uni.$u.test.mobile(value) },
							message: '手机号码不正确',
							trigger: 'blur',
						}
					],
					address: { required: true, message: '请选择地址', trigger: 'blur' },
					addr: { required: true, message: '请输入详细地址', trigger: 'blur' },
				},
				
				/* 地址选择弹窗 */
				show: false,
				provinces: [],
				citys: [],
				areas: [],
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		onLoad () {
			this.getProvinces()
			
			const eventChannel = this.getOpenerEventChannel()
			eventChannel.on('addressInfo', (data) => {
				this.form = data
			})
		},
		methods: {
			getProvinces () {
				getRegion().then(res => {
					if (res) {
						this.provinces = res.data
					}
				})
			},
			cityConfirm (e) {
				this.form.address = `${e.province.label} ${e.city.label} ${e.area.label}`
				this.form.region = e.area.value
			},
			defaultChange (e) {
				let is = 0
				if (e.detail.value) {
					is = 1
				}
				
				this.form.is_default = is
			},
			submit () {
				this.$refs.uForm.validate().then(valid => {
					console.log('valid', valid);
					if (valid) {
						uni.showLoading({ mask: true })
						editAddress(this.form).then(res => {
							uni.hideLoading({ noConflict: true })
							if (res) {
								uni.showToast({
									title: res.msg,
									icon: 'none'
								})
								
								setTimeout(() => {
									uni.navigateBack()
								}, 500)
							}
						})
					}
				}).catch(errors => {
					console.log('loginSubmit errors');
				})
			},
			deleteAddress () {
				uni.showModal({
					title: '提示',
					content: '确实删除此地址吗?',
					success: (res) => {
						if (res.confirm) {
							if (this.form.is_default) {
								uni.showToast({
									title: '默认地址不允许删除',
									icon: 'none'
								})
								return
							}
							
							uni.showLoading({ mask: true })
							deleteAddress({ id: this.form.id }).then(res => {
								uni.hideLoading({ noConflict: true })
								if (res) {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
									
									setTimeout(() => {
										uni.navigateBack()
									}, 500)
								}
							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
.address-edit {
	box-sizing: border-box;
	padding: 24rpx;
	min-height: 100%;
	background-color: $bg-color;
	.form-box {
		border-radius: 16rpx;
		padding: 0 24rpx;
		background-color: #fff;
		::v-deep {
			.u-form-item__body {
				padding: 32rpx 0;
			}
		}
	}
	.default-box {
		margin-top: 24rpx;
		border-radius: 16rpx;
		padding: 0 24rpx;
		height: 110rpx;
		background-color: #fff;
		display: flex;
		align-items: center;
		justify-content: space-between;
		.label {
			color: #333333;
			font-size: 28rpx;
		}
	}
	.button-btn {
		margin-top: 224rpx;
		padding: 0 24rpx;
		display: flex;
		gap: 22rpx;
		.delete-btn {
			width: 174rpx;
			border-radius: 80rpx;
			color: #fff;
			font-size: 28rpx;
			line-height: 80rpx;
			background-color: #d9d9d9;
		}
		.btn {
			flex: 1;
			border-radius: 80rpx;
			color: #fff;
			font-size: 28rpx;
			line-height: 80rpx;
			background-color: #5749f7;
		}
	}
}
</style>
